<?php 
namespace Home\Controller;
use Think\Controller;
/**
* 登录
*/
class EnterController extends CommonController
{
	
	public function index()
	{
		$question= D('vercode')->getOne();
		$this->assign('question',$question);
		$this->display();
	}
	public function check()
	{
		//登录验证
		$data=$_POST;
		$rec=D('user')->where("email='$data[email]'")->select();
		if ($rec) {
			$data['password']=md5($data['password']);
			$rec=D('user')->where("password='$data[password]'")->select();
			if ($rec) {
				if ($data['vercode']==$_SESSION['vercode_answer']) {
					if (isset($_SESSION['wc'])) {
						$arr=array(
							'error'=>'0',
							'info' =>'登录成功',
							'url' =>'1',
							);	
					}else{
						$arr=array(
							'error'=>'0',
							'info' =>'登录成功',
							'url' =>'0',
							);	
					}
					$data['login_time']	=time();
					D('user')->where("email='$data[email]'")->save($data);
					$rec=D('user')->where("email='$data[email]'")->select();
					$_SESSION['nikename']=$rec[0]['nikename'];
					$_SESSION['id']=$rec[0]['id'];
					$_SESSION['face']=$rec[0]['face'];
					echo json_encode($arr);
				}
				$this->checkVercode();
				
			}else{
				$arr=array(
					'error'=>'2',
					'info' =>'密码错误',
					);
					echo json_encode($arr);
					exit;
			}
		}else{
			$arr=array(
				'error'=>'1',
				'info' =>'账号错误',
				);
				echo json_encode($arr);
				exit;
		}
	}
	//转接到QQ登录页面
	public function qqlogin()
    {
        qq_login();
        
    }
    //接收QQ登录信息
    public function call()
    {
    	$openid = getOpenid();
    	$scr=M('user')->where("openid='$openid'")->select();
    	if ($scr) {
    		$_SESSION['nikename']=$scr[0]['nikename'];
			$_SESSION['id']=$scr[0]['id'];
			$_SESSION['face']=$scr[0]['face'];
			// echo "登录成功";
			$this->success('登录成功',U('home/cen/index'));
    	}else{
    		$info = getUserInfo();
    		if (isset($_SESSION['id'])) {
    			// $info['openid']=$openid;
    			
    			M('user')->where("id='$_SESSION[id]'")->save(array('openid'=>'$openid'));
    			$this->success('登录成功',U('home/cen/index'));
    			exit;
    		}
    		$info['sex']=$info['gender'];
    		
    		$scr=M('user')->where("nikename='$info[nickname]'")->select();
    		if ($scr) {
    			$info['nickname']=$info['nickname'].rand(0, 100);
    		}
    		$info['nikename']=$info['nickname'];
    		$info['openid']=$openid;
    		$info['sex']=$info['gender'];
    		$info['create_time'] =time();
            $info['create_ip'] = get_client_ip();
            $info['face']=getQQFace($info['figureurl_2']);
            $scr=M('user')->add($info);

            $_SESSION['nikename']=$info['nikename'];
            $_SESSION['id']=$scr;
			$_SESSION['face']=$info['face'];
    		$this->success('登录成功',U('home/cen/index'));
    		// echo "注册成功";
    	}
    	
    }
}

 ?>